package cn.chainco.cts.security;

import java.lang.reflect.Method;

import cn.chainco.cts.security.validator.BusinessValidator;
import cn.chainco.cts.utils.CommUtils;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.PreResultListener;

public class PreActionReturnInterceptor implements PreResultListener{
	private ActionInvocation invocation;
	private BusinessValidator businessValidator;
	private Method method;
	
	public PreActionReturnInterceptor(ActionInvocation invocation, BusinessValidator businessValidator, Method method){
		this.invocation = invocation;
		this.businessValidator = businessValidator;
		this.method = method;
		invocation.addPreResultListener(this);
	}

	public void beforeResult(ActionInvocation actionInvocation, String result){
		try{
			if(CommUtils.isValidStr(result) && !Action.ERROR.equals(result) && !Action.INPUT.equals(result)){
				int code = (Integer)method.invoke(businessValidator, new Object[]{actionInvocation});
				if(code != BinsinessInterceptor.ACCESS_PERMIT){
					//do something when interceptor error?
				}
			}
		}catch(Exception e){

		}
	}

	public ActionInvocation getInvocation(){
		return invocation;
	}

	public void setInvocation(ActionInvocation invocation){
		this.invocation = invocation;
	}

	public Method getMethod(){
		return method;
	}

	public void setMethod(Method method){
		this.method = method;
	}

	public BusinessValidator getBusinessValidator(){
		return businessValidator;
	}

	public void setBusinessValidator(BusinessValidator businessValidator){
		this.businessValidator = businessValidator;
	}

}
